﻿#include"Data.h"
Data::Data(int year, int month, int day) {
	_year = year;
	_month = month;
	_day = day;
	if (!ChackData())
	{
		cout << "⽇期⾮法" << endl;
	}
}
bool Data::ChackData()  {
	if ((_month > 12 && _month < 1) && (_day > GetMonthDay() && _day > 0))
		return true;
	else
		return false;
}
bool Data::operator<(const Data& d)const {
	if (_year < d._year) {
		return true;
	}
	if (_year == d._year && _month < d._month) {
		return true;
	}
	if (_year == d._year && _month == d._month && _day < d._day) {
		return true;
	}
	return false;
}
bool Data::operator==(const Data& d)const {
	if (_year == d._year && _month == d._month && _day == d._day) {
		return true;
	}
	return false;
}
bool Data::operator<=(const Data& d)const {
	return *this < d || *this == d;
}
bool Data::operator>(const Data& d)const {
	return !(*this <= d);
}
bool Data::operator>=(const Data& d)const {
	return !(*this < d);
}
bool Data::operator!=(const Data& d)const {
	return !(*this == d);
}

int Data::GetMonthDay()const {
	assert(_month >0 && _month<13);
	static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31};
	if (_month == 2 && (_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0) {
		return 29;
	}
	return MonthDay[_month];
}
Data& Data::operator+=(const int day) {
	_day += day;
	while (_day>GetMonthDay()) {
		_day -= GetMonthDay();
		_month++;
		if (_month == 13) {
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Data& Data::operator+(const int day)const {
	Data tmp = *this;
	return tmp+=1;
}
void Data::print()const {
	cout << _year << ' ' << _month << ' ' << _day << endl;
}
Data& Data::operator-=(const int day) {
	_day -= day;
	while (_day <= 0) {
		--_month;
		if (_month == 0) {
			_year--;
			_month = 12;
		}
		_day += GetMonthDay();
	}
	return *this;
}
Data& Data::operator-(const int day)const {
	Data tmp = *this;
	tmp -= 1;
	return tmp;
}
Data& Data::operator++() {
	*this += 1;
	return *this;
}
Data Data::operator++(int) {
	Data tmp = *this;
	*this += 1;
	return tmp;
}
Data& Data::operator--() {
	*this -= 1;
	return *this;
}
Data Data::operator--(int) {
	Data tmp = *this;
	*this += 1;
	return tmp;
}
int Data::operator-(const Data& d)const {
	Data min = *this;
	Data max = d;
	int count = 0;
	int flag = -1;
	if (*this > d) {
		min = d;
		max = *this;
		flag = 1;
	}
	while (max != min) {
		min++;
		count++;
	}
	return flag*count;
}
ostream& operator<<(ostream& out, const Data& d) {
	cout << d._year << ' ' << d._month << ' ' << d._day << endl;
	return out;
}
istream& operator>>(istream& in,  Data& d) {
	cout << "请输入年月日：" << endl;
	cin >> d._year >> d._month >> d._day;
	if (!d.ChackData()) {
		cout << "日期错误，请重新输入" << endl;
	}
	return in;
}





